const province = require('./province.js')

// 字符串转byte
function stringToBytes(str) {
  var strArray = new Uint8Array(str.length);
  for (var i = 0; i < str.length; i++) {
    strArray[i] = str.charCodeAt(i);
  }
  const array = new Uint8Array(strArray.length)
  strArray.forEach((item, index) => array[index] = item)
  return array.buffer;
}

//十六进制字符串转字节数组，跟网上demo一样
function HexString2Bytes(hex) {
  // 将发送的命令转成 Arraybuffer 类型
  var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
    return parseInt(h, 16)
  }))

  var buffer = typedArray.buffer;
  return buffer;
}

// ArrayBuffer转16进制字符串示例
function ab2hext(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('').toUpperCase();
}

// 字符串转16进制
function str2Hex(str) {
  var val = "";
  for (var i = 0; i < str.length; i++) {
    val += str.charCodeAt(i).toString(16);
  }
  return val;
}

// 数字转16进制
function decimal2Hex(str) {
  let val = "";
  for (var i = 0; i < str.length; i++) {
    let number = Number(str[i])
    if (number < 0) {
      val += 0xFFFFFFFF + number + 1;
    } else {
      val += padleft(parseInt(number).toString(16))
    }
  }
  return val
}

function padleft(str) {
  let pad = "00"
  return pad.substring(0, pad.length - str.length) + str
}

//16进制转字符串
function hexToString(str) {
  var trimedStr = str.trim();
  var rawStr =
    trimedStr.substr(0, 2).toLowerCase() === "0x" ?
    trimedStr.substr(2) :
    trimedStr;
  var len = rawStr.length;
  if (len % 2 !== 0) {
    // alert("Illegal Format ASCII Code!");
    return "";
  }
  var curCharCode;
  var resultStr = [];
  for (var i = 0; i < len; i = i + 2) {
    curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
    resultStr.push(String.fromCharCode(curCharCode));
  }
  return resultStr.join("");
}

function str2ascii(str) {
  var val = "";
  for (var i = 0; i < str.length; i++) {
    if (val == "")
      val = str.charCodeAt(i).toString(16)
    else
      val += str.charCodeAt(i).toString(16)
  }
  return val
}

/* 微信app版本比较 */
function versionCompare(ver1, ver2) {
  var version1pre = parseFloat(ver1)
  var version2pre = parseFloat(ver2)
  var version1next = parseInt(ver1.replace(version1pre + ".", ""))
  var version2next = parseInt(ver2.replace(version2pre + ".", ""))
  if (version1pre > version2pre)
    return true
  else if (version1pre < version2pre)
    return false
  else {
    if (version1next > version2next)
      return true
    else
      return false
  }
}

/*函数节流*/
function throttle(fn, interval) {
  var enterTime = 0; //触发的时间
  var gapTime = interval || 300; //间隔时间，如果interval不传，则默认300ms
  return function () {
    var context = this;
    var backTime = new Date(); //第一次函数return即触发的时间
    if (backTime - enterTime > gapTime) {
      fn.call(context, arguments);
      enterTime = backTime; //赋值给第一次触发的时间，这样就保存了第二次触发的时间
    }
  };
}

/*函数防抖*/
function debounce(fn, interval) {
  var timer;
  var gapTime = interval || 1000; //间隔时间，如果interval不传，则默认1000ms
  return function () {
    clearTimeout(timer);
    var context = this;
    var args = arguments; //保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
    timer = setTimeout(function () {
      fn.call(context, args);
    }, gapTime);
  };
}

function getPalteAlias(name) {
  let alias = null
  province.map(item => {
    if (item.eName === name) {
      alias = item.alias
    }
  })
  return alias
}

module.exports = {
  stringToBytes: stringToBytes,
  HexString2Bytes: HexString2Bytes,
  ab2hext: ab2hext,
  str2Hex: str2Hex,
  decimal2Hex: decimal2Hex,
  padleft: padleft,
  hexToString: hexToString,
  versionCompare: versionCompare,
  str2ascii: str2ascii,
  throttle: throttle,
  debounce: debounce,
  getPalteAlias: getPalteAlias
}